package com.leff.cards;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;

import com.leff.cards.ui.LoginActivity;
import com.leff.cards.util.Secrets;

public class NotificationFactory implements Secrets {
	
	public static final String UPDATED_BY_NOTIF = "AppNotifReceived";
	
	public static void buildNotif(Context C, Bundle data) {
		
		Notification N = null;
		int notifId = -1;
		
		try {
			String title = data.getString("title");
			String message = data.getString("message");
			String idStr = data.getString("id");
			notifId = Integer.parseInt(idStr);
			
			Intent action = new Intent(C, LoginActivity.class);
			PendingIntent PI = PendingIntent.getActivity(C, 0, action, Intent.FLAG_ACTIVITY_CLEAR_TOP);
			
			N = new Builder(C)
				.setContentTitle(title)
				.setSmallIcon(R.drawable.app_icon)
				.setDefaults(Notification.DEFAULT_ALL - Notification.DEFAULT_VIBRATE)
				.setAutoCancel(true)
				.setContentText(message)
				.setContentIntent(PI)
				.getNotification();
			
		} catch(Exception e) {
			Log.v(TAG, "Exception building Notification: ", e);
		}
		
		if(N != null && notifId > 0) {
			NotificationManager mgr = (NotificationManager)C.getSystemService(Activity.NOTIFICATION_SERVICE);
			mgr.cancel(notifId);
			mgr.notify(notifId, N);
		}
	}
}
